import { deviceParamsAdd, deviceParamsUpdate } from '@/services/deviceMsg';
import {
  ModalForm,
  ProFormSelect,
  ProFormText,
} from '@ant-design/pro-components';
import { Button, Col, Form, Row, message } from 'antd';
import React, { useImperativeHandle, useState } from 'react';

const ItemOption = React.forwardRef((props: any, ref) => {
  const { refreshList } = props;
  const [form] = Form.useForm<any>();
  const [modalOpen, setModalOpen] = useState(false);
  const [modItem, setModItem] = useState<any>(null);
  const handleModalShow = async (row?: any) => {
    setModItem(row || null);
    if(!modalOpen){
      form.resetFields()  
    }
    if (row) {
      form.setFieldsValue({
        ...row,
      });
    }
    setModalOpen(!modalOpen);
  };
  useImperativeHandle(ref, () => ({
    handleModalShow,
  }));
  return (
    <ModalForm
      title={modItem?.id ? '修改设备参数' : '新增设备参数'}
      width={600}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnHidden: true,
        onCancel: handleModalShow,
        centered: true,
        maskClosable: false,
      }}
      open={modalOpen}
      submitter={{
        render: (props) => {
          let optBtns: any = [
            <Button
              className="base-button"
              key="cancel"
              onClick={handleModalShow}
            >
              取消
            </Button>,
            <Button
              className="base-button"
              type="primary"
              key="submit"
              onClick={() => props.form?.submit?.()}
            >
              确定
            </Button>,
          ];
          return optBtns;
        },
      }}
      onFinish={async (values) => {
        const sendData = {
          ...values,
        };
        let res = null;
        if (modItem?.id) {
          //编辑
          res = await deviceParamsUpdate({
            ...sendData,
            id: modItem.id,
          });
        } else {
          //添加
          res = await deviceParamsAdd(sendData);
        }
        if (res?.code === 200) {
          message.success('操作成功');
          refreshList();
          setModalOpen(false);
        }
        return true;
      }}
    >
      <Row gutter={20}>
        <Col span={12}>
          <ProFormText
            name="paramName"
            label="设备参数名称"
            fieldProps={{
              maxLength: 50,
              showCount: true,
            }}
            rules={[
              {
                required: true,
                message: '请输入设备参数名称',
              },
            ]}
          />
        </Col>
        <Col span={12}>
          <ProFormText
            name="paramCode"
            label="设备参数编码"
            fieldProps={{
              maxLength: 50,
              showCount: true,
            }}
            rules={[
              {
                required: true,
                message: '请输入设备参数名称',
              },
            ]}
          />
        </Col>
      </Row>
      <Row gutter={20}>
        <Col span={24}>
          <ProFormSelect
            name="paramValue"
            label="设备参数键值"
            placeholder="请选择设备参数键值"
            allowClear
            rules={[
              {
                required: true,
                message: '请选择设备参数键值',
              },
            ]}
            request={async (params) => {
              const { paramCode } = params;
              if (!paramCode) {
                return [];
              } else {
                if (paramCode === '1') {
                  //走纸速度
                  return [
                    { label: '12.5mm/s', value: '12.5mm/s' },
                    { label: '25mm/s', value: '25mm/s' },
                    { label: '50mm/s', value: '50mm/s' },
                  ];
                } else if (paramCode === '2') {
                  //增益
                  return [
                    { label: '1/4', value: '1/4' },
                    { label: '1/2', value: '1/2' },
                    { label: '1', value: '1' },
                    { label: '2', value: '2' },
                  ];
                } else if (paramCode === '3') {
                  // 导联号：1：I导；2：II导；3：III导  4：aVR导；5：aVL导；6：aVF导；7：V1导；8：V2导；9：V3导；10：V4导；11：V5导；12：V6导
                  return [
                    { label: 'I', value: '1' },
                    { label: 'II', value: '2' },
                    { label: 'III', value: '3' },
                    { label: 'aVR', value: '4' },
                    { label: 'aVL', value: '5' },
                    { label: 'aVF', value: '6' },
                    { label: 'V1', value: '7' },
                    { label: 'V2', value: '8' },
                    { label: 'V3', value: '9' },
                    { label: 'V4', value: '10' },
                    { label: 'V5', value: '11' },
                    { label: 'V6', value: '12' },
                  ];
                } else if (paramCode === '4') {
                  // 导联号：1：I导；2：II导；3：III导  4：aVR导；5：aVL导；6：aVF导；7：V1导；8：V2导；9：V3导；10：V4导；11：V5导；12：V6导
                  return [
                    { label: 'I', value: '1' },
                    { label: 'II', value: '2' },
                    { label: 'III', value: '3' },
                    { label: 'aVR', value: '4' },
                    { label: 'aVL', value: '5' },
                    { label: 'aVF', value: '6' },
                    { label: 'V1', value: '7' },
                    { label: 'V2', value: '8' },
                    { label: 'V3', value: '9' },
                    { label: 'V4', value: '10' },
                    { label: 'V5', value: '11' },
                    { label: 'V6', value: '12' },
                  ];
                }
              }
              return [];
            }}
            dependencies={['paramCode']}
          />
        </Col>
      </Row>
    </ModalForm>
  );
});

export default ItemOption;
